const Layer = require("./layer");
function Route() {
  this.stack = []; // 用来存放里层的layer
}
Route.prototype.dispatch = function (req, res, out) {
  let idx = 0;
  let next = () => {
    if (idx >= this.stack.length) return out(); // 递归的终止条件
    let layer = this.stack[idx++];
    if (layer.method === req.method.toLowerCase()) {
      // 用户写的逻辑
      layer.handle_request(req, res, next);
    } else {
      next();
    }
  };
  next();
};
Route.prototype.get = function (handlers) {
  handlers.forEach((handler) => {
    const layer = new Layer("/XXXXXX", handler);
    layer.method = "get";
    this.stack.push(layer);
  });
};
module.exports = Route;
